package com.djzhou.gmms.department.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.opensymphony.xwork2.ModelDriven;

public class DepartmentAction extends BaseAction<TbDepartment> implements ModelDriven<TbDepartment> {
	
	private static final long serialVersionUID = 1L;
	private TbDepartment department;
	private Integer departmentId;
	private Integer companyId;
	private List<TbDepartment> list;
	
	private String departmentName;
	
	private List<TbDepartment> departmentList;
	private List<TbCompany> companyList; 

	public String query4AutoComplete(){
		HashMap<String,Object> hashMap=new HashMap<String, Object>();
		if(departmentName!=null && !departmentName.equals(""))hashMap.put("departmentName", departmentName);
		resource=departmentService.query4AutoComplete(hashMap,sidx,sord,rows,page);
		return SUCCESS;
	}
	public String configDefault(){
		departmentService.updateDepartmentDefault(departmentId);
		msg="设置成功";
		setCurrentPage(currentPage);
		logService.addLog("部门管理",queryUser(),"设置默认",departmentId,"TbDepartment",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	@Override
	public String delete() {
		setDepartmentName(departmentName);
		departmentService.delete(departmentId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("部门管理",queryUser(),"删除",departmentId,"TbDepartment",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		setDepartmentName(departmentName);
		TbCompany company=companyService.queryById(companyId);
		department.setCompany(company);
		department.setLastModifyTime(new Date());
		department.setUser(queryUser());
		departmentService.update(department);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("部门管理",queryUser(),"编辑",departmentId,"TbDepartment",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		companyList=companyService.queryList(0);
		if(departmentName!=null && !departmentName.equals(""))hashMap.put("departmentName", departmentName);
		hashMap.put("companyId", queryCompany().getCompanyId());
		pageBean=departmentService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=departmentService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbDepartment>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		setDepartmentName(departmentName);
		
		if(department!=null){
			TbCompany company=companyService.queryById(companyId);
			Date date=new Date();
			department.setCompany(company);
			department.setCreateTime(date);
			department.setLastModifyTime(date);
			department.setDeleteFlag(0);
			department.setUser(queryUser());
			departmentService.add(department);
		}
		setCurrentPage(currentPage);
		logService.addLog("部门管理",queryUser(),"添加",departmentId,"TbDepartment",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		companyList=companyService.queryList(0);
		setDepartmentName(departmentName);
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),departmentId);//查询当前用户所在公司下的部门(除了自己)
		companyList=companyService.queryList(0);
		setDepartmentName(departmentName);
		setCurrentPage(currentPage);
		department=departmentService.queryById(departmentId);
		return SUCCESS;
	}

	public TbDepartment getModel() {
		return department;
	}

	public TbDepartment getDepartment() {
		return department;
	}

	public void setDepartment(TbDepartment department) {
		this.department = department;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public List<TbDepartment> getList() {
		return list;
	}

	public void setList(List<TbDepartment> list) {
		this.list = list;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public List<TbDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TbDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public List<TbCompany> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<TbCompany> companyList) {
		this.companyList = companyList;
	}

	public Integer getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Integer companyId) {
		this.companyId = companyId;
	}
	

}
